<template>
	<div class="form-control">
		<el-form :model="user" label-width="auto" style="width: 100%">
			<el-form-item label="原密码">
				<el-input
					 v-model="user.oldPassword"
					 type="password"
					 placeholder="请输入原密码"
				  />
			</el-form-item>
			<el-form-item label="新密码">
				<el-input
					 v-model="user.password"
					 type="password"
					 placeholder="请输入新密码"
				  />
			</el-form-item>
			<el-form-item label="确认密码">
				<el-input
					 v-model="user.password2"
					 type="password"
					 placeholder="请再次输入新密码"
				  />
			</el-form-item>
			<el-form-item>
				<div class="button-group">
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="cancel">关闭</el-button>
				</div>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
import API from '@/api'
import { ElMessage } from 'element-plus'

export default {
	data(){
		return {
			user: {
				oldPassword: "",
				password: "",
				password2: ""
			}
		}
	},
	methods: {
		async save() {
			if (this.user.password !== this.user.password2) {
				ElMessage.error('两次输入的新密码不一致！')
				return
			}
			try {
				await API.modifyPassword(this.user)
				ElMessage.success('密码修改成功！')
				// 可以选择关闭弹窗或刷新页面
				this.$emit('close')
			} catch (error) {
				console.error('密码修改失败:', error)
				ElMessage.error('密码修改失败，请检查原密码或稍后再试。')
			}
		},
		cancel() {
			this.$emit('close')
		}
	}
}
</script>

<style scoped>
.form-control {
	padding: 30px 30px 10px;
}
.button-group {
    display: flex;
    justify-content: flex-end;
    width: 100%;
}
</style>